package com.pic.mgt.telnetclient;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.telnet.TelnetClient;

import com.pic.mgt.utils.RunTimeSystemUtils;

public final class IgxShellCmdSender implements Runnable {
	private String telnetIP;
	private int telnetPort;
	private int responseTimeout = 5000;
	
	private TelnetClient telnetClient;
	private InputStream inputStream;
	private OutputStream outputStream;
	private String responseMsg = null;
	
	public IgxShellCmdSender() {
		this.telnetClient = new TelnetClient();
	}
	
	/**
	 * 送出 shell command 指令。
	 * @param shellCommandStr
	 * @return 回傳訊息。
	 */
	public String sendShellCommand(String shellCommandStr) throws IgxShellCmdSendException {
		try {
			// throws SocketException, IOException
			this.telnetClient.connect(telnetIP, telnetPort);
			this.outputStream = this.telnetClient.getOutputStream();
			this.inputStream = this.telnetClient.getInputStream();
			
			
			// throws IOException
			getResponseMsg();
			
			
			// 送出 shell command
			this.outputStream.write((shellCommandStr + (char)10).getBytes());
			this.outputStream.flush();
			
			// 啟動回應時間等待 thread
			new Thread(this).start();
			
			// throws IOException
			this.responseMsg = getResponseMsg();
			
			closeConnection();
			
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	@Override
	public void run() {
		// 等待回應時間
		RunTimeSystemUtils.threadSleep(this.responseTimeout);
		
		if (null == this.responseMsg) {
			// 超過回應等待時間，將中斷連線。
			closeConnection();
		}
	}
	
	private String getResponseMsg() throws IOException {
		byte[] buff = new byte[1024];
        int ret_read = 0;
        
        StringBuffer responseMsgStrBuf = new StringBuffer();
        
        int readInt;
        
        do
        {
        	readInt = this.inputStream.read();
        	if (readInt == 18) {
        		System.out.println(responseMsgStrBuf.toString());
        		break;
        	}
        	else if (readInt>=32 && readInt<=127) {
        		responseMsgStrBuf.append((char)readInt);
        	}
        	else {
        		
        	}
        }
        while (readInt != -1);
        
        return responseMsgStrBuf.toString();
	}
	
	private void closeConnection() {
		IOUtils.closeQuietly(this.inputStream);
		IOUtils.closeQuietly(this.outputStream);
		
		try {
			this.telnetClient.disconnect();
		} catch (IOException e) {
			
		}
	}
	
	// --- getter and setter 
	
	public void setTelnetIP(String telnetIP) {
		this.telnetIP = telnetIP;
	}
	public void setTelnetPort(int telnetPort) {
		this.telnetPort = telnetPort;
	}
	public void setConnectionTimeout(int connectionTimeout) {
		this.telnetClient.setConnectTimeout(connectionTimeout);
	}
	public void setResponseTimeout(int responseTimeout) {
		this.responseTimeout = responseTimeout;
	}
	
	public static void main(String args[]) {
		IgxShellCmdSender sender = new IgxShellCmdSender();
		
		try {
			sender.setTelnetIP("localhost");
			sender.setTelnetPort(9011);
			String responseMsg = sender.sendShellCommand("gj");
		} catch (IgxShellCmdSendException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
